

import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';
import 'package:modal_bottom_sheet/modal_bottom_sheet.dart';
import 'package:xim/common/style/colors.dart';
import 'package:xim/common/util/collection.dart';
import 'package:xim/common/util/device.dart';
import 'package:xim/config/route_config.dart';

class ToastUtil {

  static void snackbar({required String title, required String content, BuildContext? context, Color? colorText}){
    var textColor = context!=null?ModelColor(
        darkColor: const Color(0xffcfcfcf),
        lightColor: const Color(0xff171717),
        model: PlatformUtils.getBrightness(context)
    ) : ModelColor(
        darkColor: const Color(0xffcfcfcf),
        lightColor: const Color(0xff171717),
        model: Brightness.light
    );

    Get.snackbar(
      title,
      content,
      colorText: colorText??textColor.color,
      duration: const Duration(seconds: 2)
    );
  }

  static void dialog(String title, String content, {
    bool barrierDismissible=true,
    TextStyle? titleStyle,
    TextStyle? middleTextStyle,
  }){
      Get.defaultDialog(
        title: title,
        middleText: content,
        middleTextStyle: middleTextStyle,
        radius: 20,
        titleStyle:titleStyle,
        barrierDismissible:barrierDismissible,
      );
  }

  static void waringDialog(String title,{
    Icon? icon,
    String? content,
    bool barrierDismissible=true,
    TextStyle? titleStyle,
    TextStyle? middleTextStyle,
    Widget? customContent,
    Color? bgColor,
    List<Widget>? actions
  }){
    Get.dialog(
        AlertDialog(
          backgroundColor: bgColor,
          icon: icon,
          title: Text(title, textAlign: TextAlign.center, style: titleStyle),
          content: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisSize: MainAxisSize.min,
            children: [
              if (customContent!=null) customContent,
              if (customContent==null && content!=null) Text(content),
            ],
          ),
          actions: actions,
          actionsAlignment: MainAxisAlignment.center,
      )
    );
  }


  static void bottomSheet({required List<Widget> children, Color? bgColor, bool overlay=true, EdgeInsets? padding, BuildContext? context}){
    context ??= Get.context;
    showMaterialModalBottomSheet(
      context: context!,
      backgroundColor: Colors.transparent,
      builder: (context) => Container(
        padding: padding??const EdgeInsets.symmetric(vertical: 18, horizontal: 4),
        decoration: BoxDecoration(
            color: bgColor,
            borderRadius: const BorderRadius.only(
                topLeft: Radius.circular(8),
                topRight: Radius.circular(8)
            )
        ),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: children,
        ),
      ),
    );
  }

  static void bottomSelectedSheet({required List<Widget> children, double gap=16, bool overlay=true,
    EdgeInsets? padding, BuildContext? context, bool showCancel=true, TextStyle? cancelTxtStyle,
    Widget? cancelDivider, bool showCancelDivider=true}){
    bottomSheet(
        children: [
          ...children
              .splitJoin(SizedBox(height: gap,))
              .toList(),
          if(showCancel) ...[
            if(showCancelDivider) ...[
              SizedBox(height: gap,),
              Container(height: 0.6, color: const Color(0xffe6e6e6),),
              SizedBox(height: gap,),
            ],
            if(!showCancelDivider) SizedBox(height: gap,),
            GestureDetector(
              onTap: ()=>Routers.back(),
              child: Container(
                color: Colors.transparent,
                child: Center(
                  child: Padding(
                    padding: const EdgeInsets.symmetric(vertical: 2),
                    child: Text("取消", style: cancelTxtStyle,),
                  ),
                ),
              ),
            ),
          ]
        ],
        overlay: overlay,
        padding: padding,
        context: context
    );
  }

  static void toast(String msg, {BuildContext? context}){
    var brightness = context!=null?PlatformUtils.getBrightness(context):Brightness.light;
    Fluttertoast.showToast(
        msg: msg,
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.BOTTOM,
        timeInSecForIosWeb: 1,
        backgroundColor: brightness==Brightness.light?const Color(0xfff0f0f0):const Color(0xff4f4f4f),
        textColor: brightness==Brightness.light?const Color(0xff4f4f4f):const Color(0xfff0f0f0),
        fontSize: 14.0
    );
  }

}